import { configuration } from '../../config/webapi';
import View from '../View';
import event from '../../util/NotifierManager';
import {
    INDEX_HEAT, INDEX_FURNACE,
} from '../../util/constant';

/**
 * 图扑熔保炉
 *
 * @export
 * @class Index2dFurnace
 * @extends {View}
 */
export default class Index2dFurnace extends View {

    constructor(view, main) {
        super(view, main);

        this.url = 'displays/tenant/OPCUAWebAPIss/Dev/熔保炉.json'; // 图扑熔保炉：displays/tenant/OPCUAWebAPIss/Dev/产品标识.json
        this.key = INDEX_FURNACE;

        this.tags = ['furnace_chart1', 'furnace_chart2', 'naviNode'];
        this.btnTags = [];
        this.tags = this.tags.concat(this.btnTags);

        this.tagMap = {
            temperatures: [], // 15
            pressures: [], // 10

            furnace_chart1_1: [],
            furnace_chart1_2: [],

            furnace_chart2_1: [],
            furnace_chart2_2: [],
        }

        window.index2dFurnace = this;
    }

    onPostDeserialize() {
        if (!ht.animationplayer.g2d) {
            ht.animationplayer.g2d = this.view;
            ht.animationplayer.player2d = new ht.animationplayer.blueprintplayer(view);
            ht.animationplayer.player2d.init();
        } else {
            ht.animationplayer.player2d.init(true);
        }

        this.getDatasByTag();
        // 所有温度 node
        for (let i = 1; i <= 13; i++) {
            const tag = 'temp' + i;
            this[tag] = this.dm.getDataByTag(tag);
        }
        // 所有压力 node
        for (let i = 1; i <= 4; i++) {
            const tag = 'press' + i;
            this[tag] = this.dm.getDataByTag(tag);
        }


        this.naviNode.a('onStateChange', (state) => {
            this.onStateChange(state);
        });

        this.startRefreshTask();
        this.readAll();
    }

    handleInteractive(e) {
        const { kind, data } = e;
        if (kind == 'doubleClickBackground') {
            return;
        }
        if (kind == 'clickData') {
            this.handleClickData(data);
        }

    }

    handleClickData(data) {
        // console.log(data);
    }

    /******************************* OPCUA BEGIN ******************************** */

    handleUpdateBooleanNode(tag, valueObj) {
    }

    handleUpdateNumberNode(tag, valueObj) {

    }

    /**
     *
     *
     * @param {*} tag tagMap中的tag
     * @param {*} valueObj .Value = [0.1,...], 数值范围是0~1之间的随机数
     * @memberof Index2dHeat
     */
    handleUpdateArrayNode(tag, valueObj) {
        // console.log('-----handleUpdateArrayNode-----', { tag, valueObj })

        switch (tag) {
            case 'furnace_chart1_1':
                this.furnace_chart1.a('lineData1', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'furnace_chart1_2':
                this.furnace_chart1.a('lineData2', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'furnace_chart2_1':
                this.furnace_chart2.a('lineData1', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;
            case 'furnace_chart2_2':
                this.furnace_chart2.a('lineData2', valueObj.Value.map((v) => { return v.toFixed(0) }));
                break;

            case 'temperatures':
                for (let i = 1; i <= 13; i++) {
                    const tag = 'temp' + i;
                    if (!this[tag]) {
                        continue;
                    }
                    this[tag].s('text', valueObj.Value[i].toFixed(0));
                    this[tag].a('price', valueObj.Value[i].toFixed(0));
                }
                break;
            case 'pressures':
                for (let i = 1; i <= 4; i++) {
                    const tag = 'press' + i;
                    if (!this[tag]) {
                        continue;
                    }
                    this[tag].s('text', valueObj.Value[i].toFixed(0));
                    this[tag].a('price', valueObj.Value[i].toFixed(0));
                }
                break;
            default:
                break;
        }
    }



}
